package org.sixdog.mcsit.service.impl;

import org.sixdog.mcsit.service.TokenService;
import org.sixdog.mcsit.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.concurrent.TimeUnit;
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public boolean addToken(String token, String tokenValue, long timeMinute) {
        redisTemplate.opsForValue().set(token,tokenValue,timeMinute, TimeUnit.MINUTES);
        return true;
    }
    @Override
    public boolean hasToken(String tokenKey) {
        return redisTemplate.hasKey(tokenKey);
    }
    @Override
    public String getTokenValue(String tokenKey) {
        if (!StringUtils.isBlank(tokenKey)){
            String value = (String)redisTemplate.opsForValue().get(tokenKey);
            return value;
        }
        return null;
    }

    @Override
    public boolean setTokenTime(String tokenKey, int timeMinute) {
        redisTemplate.expire(tokenKey,timeMinute,TimeUnit.MINUTES);
        return true;
    }

}
